//
//  DetailOfCircleViewController.m
//  LoveOfPet
//
//  Created by 芦彬 on 15/11/17.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "DetailOfCircleViewController.h"
#import "LuBDefine.h"
#import "LBAFNetworking.h"
#import "DetailOfCircleTableViewCell.h"
#import "DetailOfCircle.h"
#import "CommentsTableViewCell.h"
#import "DetailOfUserViewController.h"
#import <MJRefresh.h>
#import "SDWebImageManager.h"
#import "UIImageView+WebCache.h"

@interface DetailOfCircleViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic ,retain)UITableView *tableView;

@property (nonatomic, retain)NSMutableArray *detailOfCricleArray;

@property (nonatomic, assign)NSInteger size;

@property (nonatomic, assign)BOOL isUpLoading;

@end

@implementation DetailOfCircleViewController

- (void)dealloc{
    [_tableView release];
    [_recommend release];
    [_detailOfCricleArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forBarMetrics:UIBarMetricsCompact];
    [self createDetailOfCircleViewController];
    [self downloadData:self.size];
    // Do any additional setup after loading the view.
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.detailOfCricleArray = [NSMutableArray array];
    }
    return self;
}

- (void)downloadData:(NSInteger)size{
    NSString *tagid = [self.recommend tag_id];
    NSString *url = [NSString stringWithFormat:@"http://api.epetbar.com/gutouv2/photo.html?tag_id=%@&do=thememore&page=%ld&sort=1&limit=10&develop=0",tagid, self.size];
//    NSLog(@"%@",url);
    [LBNetWork handleGETWithUrlString:url parameters:nil showHuD:YES onView:nil successfulBlock:^(id responseObject) {
        NSArray *detailOfCircleArray = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in detailOfCircleArray) {
            DetailOfCircle *detailOfCircle = [DetailOfCircle detailOfCircleWithDictionary:dic];
            [self.detailOfCricleArray addObject:detailOfCircle];
        }
        [self.tableView reloadData];
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
        // 没有更多数据
        if (detailOfCircleArray.count == 0) {
            [self.tableView.footer endRefreshingWithNoMoreData];
        }
    } failureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}

- (void)createDetailOfCircleViewController{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = [self.recommend tag_name];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(5, 64, CWIDTH - 10, CHEIGHT - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor whiteColor];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    if(self.detailOfCricleArray.count){
        NSURL *imageViewUrl = [NSURL URLWithString:[self.recommend cover]];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, CWIDTH, CWIDTH)];
        [imageView sd_setImageWithURL:imageViewUrl placeholderImage:[UIImage imageNamed:@"zhanweitu.jpg"]];
        self.tableView.tableHeaderView = imageView;
        [imageView release];
    }
    [self addHeaderRefresh];
    [self addFooterUploading];
}

- (void) addHeaderRefresh{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.size = 1;
        self.isUpLoading = NO;
        [self downloadData:self.size];
    }];
}

- (void) addFooterUploading{
    self.tableView.footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        self.size += 1 + 1;
        self.isUpLoading = YES;
        [self downloadData:self.size];
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.detailOfCricleArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //一共3个cell 每个分区
    return [[self.detailOfCricleArray objectAtIndex:section] commentsArray].count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        if (self.detailOfCricleArray.count == 0) {
            return 0;
        }else{
            DetailOfCircleTableViewCell *detailOfCircleCell = [DetailOfCircleTableViewCell detailOfCircleTableViewCellWithTableView:tableView];
            detailOfCircleCell.detailOfCircle = self.detailOfCricleArray[indexPath.section];
            return detailOfCircleCell;
        }
    }else{
        CommentsTableViewCell *commentsCell = [CommentsTableViewCell commentsTableViewWithTableView:tableView];

        commentsCell.comments = [[[self.detailOfCricleArray objectAtIndex:indexPath.section] commentsArray] objectAtIndex:indexPath.row - 1];
        return commentsCell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section{
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CWIDTH, 40)];
    bgView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, CWIDTH - 30, 30)];
    label.font = [UIFont systemFontOfSize: 14];
    label.backgroundColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    if ([[self.detailOfCricleArray objectAtIndex:section] commentsArray].count < 2) {
        label.text = @"💬 没有更多评论";
    }else{
        label.text = @"💬 更 多 评 论";
    }
    label.textColor = [UIColor grayColor];
    [bgView addSubview:label];
    [label release];
    return [bgView autorelease];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return CHEIGHT / 5 * 3 + 140;
    }else{
        return 70;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailOfUserViewController *detailOfUserViewController = [[DetailOfUserViewController alloc]init];
    detailOfUserViewController.detailOfCricle = self.detailOfCricleArray[indexPath.section];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailOfUserViewController animated:YES];
    [detailOfUserViewController release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidAppear:(BOOL)animated{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"2.jpg"] forBarMetrics:UIBarMetricsCompact];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
